import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: () => import('../views/login'),
    
  },
  {
    path: '/home',
    redirect:'/welcome',
    meta:{auth:true},
    component: () => import('../views/home'),
    children:[
      {
        path: '/welcome',
        component: () => import('../views/welcome')
      },
      {
        path: '/users',
        component: () => import('../views/users')
      },
      {
        path: '/roles',
        component: () => import('../views/roles')
      },
      {
        path: '/rights',
        component: () => import('../views/rights')
      },
      {
        path: '/goods',
        component: () => import('../views/goods')
      },
      {
        path: '/goods_add',
        component: () => import('../views/goods_add')
      },
      {
        path: '/reports',
        component: () => import('../views/reports')
      },
    ]
  },
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
    console.log('to:', to)
    const flag = to.matched.some(item => {
      return item.meta.auth
    })
  
    console.log('flag:', flag)
    if (flag) {
      const token = sessionStorage.getItem('token')
      if (!token) {
        next({
          path: '/'
        })
      } else {
        next()
      }
    } else {
      next()
    }
})
export default router
